import QtQuick 6.2
import QtQuick.Controls 6.2


Item{
    id:root
    width:   1200
    height: 600
    property int navLabNum: 5
    property var menuName: [{name:"文件",strBid:"fileBtnId",shortCut:"Ctrl+H",subMenuId:fileMenu},
                            {name:"视图",strBid:"viewBtnId",shortCut:"Ctrl+E",subMenuId:viewMenu},
                            {name:"编辑",strBid:"editBtnId",shortCut:"Ctrl+K",subMenuId:editMenu}]
    property string backColor:"#58e67e"
    property string selectColor:"white"
    property double lineHeightFactor: 0.045
    property double wordHeightFactor: 0.032


    function getRootHeight(){
        return lineHeightFactor * root.height;
    }

    function getRootWidth(){
        return parent.width;
    }

    signal navBarClickMessage(int n,string message,int index)
    signal changeNavBarShowMenu(int oldShow,int newShow)


    Rectangle {

        id:navContainer
        width: parent.width
        height: lineHeightFactor * root.height
        color: root.backColor


        Flow{
            id: navline
            topPadding: root.height*0.032 / navLabNum
            spacing: root.height*0.032 / navLabNum

            Repeater{
                id:menuRepeater
                model: menuName
                Rectangle {
                    width: Math.max(root.width*0.05,modelData.name.length*16)
                    height: root.height*wordHeightFactor
                    property int mid: index
                    //是否被点击 1点击 0 未点击
                    property int selected : 0
                    color: root.backColor
                    Text {
                        text: modelData.name
                        anchors.centerIn: parent
                    }
                    Loader {
                        anchors.left: parent.left
                        anchors.top: parent.top
                        anchors.topMargin: parent.height+(lineHeightFactor-wordHeightFactor)/2 * root.height
                        source : ""
                    }
                    MouseArea {
                        anchors.fill: parent
                        hoverEnabled: true
                        onEntered: {
                            parent.color = root.selectColor;
                            if(GlobalModel.clickType==1 && GlobalModel.curShowNavBar!=-1){
                                console.debug("hre:::sbar="+GlobalModel.curShowNavBar+";ctype="+GlobalModel.clickType)
                                if(GlobalModel.curShowNavBar==index){
                                    return
                                }
                                hideSubMenu(GlobalModel.curShowNavBar);
                                showSubMenu(index)
                                changeNavBarShowMenu(GlobalModel.curShowNavBar,index);
                            }else {
                                console.debug("www:::sbar="+GlobalModel.curShowNavBar+";ctype="+GlobalModel.clickType)
                                hideSubMenu(index);
                                hideSubMenu(GlobalModel.curShowNavBar);
                                changeNavBarShowMenu(GlobalModel.curShowNavBar,-1);
                            }
                        }
                        onExited: {
                            parent.color=root.backColor

                        }
                        onClicked: {
                            console.log("clicked:sbar="+GlobalModel.curShowNavBar+";ctype="+GlobalModel.clickType)
                            if(GlobalModel.curShowNavBar==index){
                                hideSubMenu(index);
                                changeNavBarShowMenu(GlobalModel.curShowNavBar,-1);
                                navBarClickMessage(0,"navBarClickedClosed",index);
                            }else{
                                showSubMenu(index);
                                if(GlobalModel.curShowNavBar!=-1){
                                    hideSubMenu(GlobalModel.curShowNavBar);
                                }
                                changeNavBarShowMenu(GlobalModel.curShowNavBar,index);
                                navBarClickMessage(1,"navBarClicked",index);
                            }
                        }
                    }
                    Action{
                        shortcut: modelData.shortCut
                        onTriggered:{
                           menuRepeater.itemAt(index).color = root.selectColor;
                        }
                    }

                    function showSubMenu(n){
                        menuRepeater.itemAt(n).children[1].sourceComponent=menuName[n].subMenuId
                        selected=1
                        //menuRepeater.itemAt(n).loadMenu="";
                        /*
                        for(var i=0;i<children.length;i++){
                            console.log("Child", i, ":", children[i]+";"+loadMenu+";"+n);
                        }
                        */
                    }

                    function hideSubMenu(n){
                        if(n>=0){
                            console.log("hideSubMenu:"+n);
                            selected=0
                            menuRepeater.itemAt(n).children[1].sourceComponent=null
                        }

                    }
                }

            }

        }

    }


    Component {
        id:fileMenu
        Rectangle{
            width: 100
            height: 50
            z:2
            NavItem {
                dataObj:[{
                        "title": "新建文件",
                        "suffix": "ctl+a",
                        "iconUrl": "",
                    }, {
                        "title": "新建文件夹",
                        "suffix": "ctl+h",
                        "iconUrl": "",
                    }, {
                        "title": "保存文件",
                        "suffix": "ctl+e",
                        "iconUrl": "",
                    },
                    {
                        "title": "保存文件夹",
                        "suffix": "ctl+e",
                        "iconUrl": "",
                    }
                ]
            }
        }

    }

    Component {
        id:viewMenu
        Rectangle{
            width: 100
            height: 50
            z:2
            NavItem {
                dataObj:[{
                        "title": "模板视图",
                        "suffix": "ctl+a",
                        "iconUrl": "",
                    }, {
                        "title": "文件视图",
                        "suffix": "ctl+h",
                        "iconUrl": "",
                    }, {
                        "title": "工具视图",
                        "suffix": "ctl+e",
                        "iconUrl": "",
                    },
                    {
                        "title": "关闭所有视图",
                        "suffix": "ctl+e",
                        "iconUrl": "",
                    }
                ]
            }
        }

    }

    Component{
        id:editMenu
        Rectangle{
            width: 100
            height: 50
            z:2
            NavItem {
                dataObj:[{
                        "title": "撤销",
                        "suffix": "ctl+a",
                        "iconUrl": "",
                    }, {
                        "title": "从剪切板历史中粘贴",
                        "suffix": "ctl+h",
                        "iconUrl": "",
                    }, {
                        "title": "查找/替换",
                        "suffix": "ctl+e",
                        "iconUrl": "",
                    },
                    {
                        "title": "跳转到指定行",
                        "suffix": "ctl+e",
                        "iconUrl": "",
                    }
                ]
            }

        }
    }
}




